package com.etoak.config;

import cn.hutool.core.util.StrUtil;
import com.etoak.common.core.properties.ImageProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.config.ResourceHandlerRegistry;
import org.springframework.web.reactive.config.WebFluxConfigurer;

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {

    @Autowired
    ImageProperties imageProperties;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // mapping = /pics/**
        String mapping = imageProperties.getMapping().endsWith(StrUtil.SLASH) ?
                imageProperties.getMapping() + "**" :
                imageProperties.getMapping() + "/**";

        // location = file:d:/upload/et2407/
        String location = imageProperties.getLocation().endsWith(StrUtil.SLASH) ?
                "file:" + imageProperties.getLocation() :
                "file:" + imageProperties.getLocation() + StrUtil.SLASH;

        registry.addResourceHandler(mapping).addResourceLocations(location);
    }
}
